/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xuexiang.xui_lib.component.banner;

import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.app.Context;

import com.xuexiang.xui_lib.component.banner.transform.PageTransformer;

import java.util.HashMap;
import java.util.Map;

/**
 * PageSlider
 *
 * @since 2021-05-06
 */
public class MyPageSlider extends ohos.agp.components.PageSlider
    implements ohos.agp.components.PageSlider.PageChangedListener {
    protected Map<Integer, Component> mViewCache = new HashMap<>();
    private static final int pageSwitchTime = 900;
    private static final int negative = -1;
    private PageTransformer mPageTransformer;
    private PageChangedListener mPageChangedListener;
    /**
     * Viewpager 滑动进度
     */
    //private float itemPosOffset = 0;
    /**
     * Viewpager 滑动像素
     */
    //private int itemPosOffsetPixels = 0;
    /**
     * Viewpager 当前定位
     */
//    private int currentItemPos;

    /**
     * 构造
     *
     * @param context 上下文
     */
    public MyPageSlider(Context context) {
        super(context);
        init();
    }

    /**
     * 构造
     *
     * @param context 上下文
     * @param attrSet 属性
     */
    public MyPageSlider(Context context, AttrSet attrSet) {
        super(context, attrSet);
        init();
    }

    /**
     * 构造
     *
     * @param context 上下文
     * @param attrSet 属性
     * @param styleName styleName
     */
    public MyPageSlider(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        init();
    }

    private void init() {
        setPageSwitchTime(pageSwitchTime); // 设置翻页时间
        addPageChangedListener(this);
    }

    /**
     * onPageSliding
     *
     * @param itemPos 位置
     * @param itemOffset 偏移
     * @param itemOffsetPixels 像素
     */
    @Override
    public void onPageSliding(int itemPos, float itemOffset, int itemOffsetPixels) {
        if (mPageTransformer != null) {
            if (itemOffsetPixels > 0) {
                scrollLeft(itemPos, itemOffset);
            } else {
                scrollRight(itemPos, itemOffset);
            }
        }
    }

    private void scrollLeft(int position, float positionOffset) {
        Component in = mViewCache.get(position + 1);
        Component out = mViewCache.get(position);
        mPageTransformer.transformPage(in, 1 - positionOffset);
        mPageTransformer.transformPage(out, 0 - positionOffset);
    }

    private void scrollRight(int position, float positionOffset) {
        if (position == 0) {
            return;
        }
        Component in = mViewCache.get(position - 1);
        mPageTransformer.transformPage(in, negative + positionOffset);
        Component out = mViewCache.get(position);
        mPageTransformer.transformPage(out, positionOffset);
    }

    @Override
    public void onPageSlideStateChanged(int state) {
        if (mPageChangedListener != null) {
            mPageChangedListener.onPageSlideStateChanged(state);
        }
    }

    @Override
    public void onPageChosen(int itemPos) {
        if (mPageChangedListener != null) {
            mPageChangedListener.onPageChosen(itemPos);
        }
    }

    /**
     * 设置动画
     *
     * @param pageTransformer 动画
     */
    public void setPageTransformer(PageTransformer pageTransformer) {
        this.mPageTransformer = pageTransformer;
    }

    /**
     * 设置Page切换监听器
     *
     * @param pageChangedListener 监听器
     */
    public void setPageChangedListener(PageChangedListener pageChangedListener) {
        this.mPageChangedListener = pageChangedListener;
    }
}
